<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@include file="../../taglibs.jsp" %>

<script type="text/javascript">
	$(function(){
		var trNum = 1;
		/**
		 * 新建订单的名词
		 */
		$("#add").click(function(){
			trNum++;
			//获得tr1的内部html代码
			var trNew = $("#tr1").html();
			//拼装tr
			trNew = '<tr id="tr'+trNum+'" align="center" bgcolor="#FFFFFF">'+trNew+'</tr>';
			trNew = trNew.replace(/#tr1/g, "#tr"+trNum);
			
			//把html代码转换成jQuery对象
			var productSelectOptHtml = $(trNew).find(".goods").html();
			trNew = trNew.replace(productSelectOptHtml, "");
			
			//把新加的tr追加到总计前面
			$("#finalTr").before(trNew);
			
		});
		
		
		$("#supplierId").change(function(){
			//获得供应商的id
			var supplierId = $(this).val();
			$.ajax({
				url:"${path}/product_loadpt",
				type:"post",
				dataType:"text",
				data:"erpQuery.supplierId="+supplierId,
				success:function(responseText){
					//通过jQuery解析json格式的字符串变成json对象
					$(".goodsType").empty();
					var jsonObj = $.parseJSON(responseText);
					for(var i = 0 ; i < jsonObj.length; i++){
						$(".goodsType").append("<option value='"+jsonObj[i].productTypeId+"'>"+jsonObj[i].name+"</option>");
					}
					
					var trObj = $("#tr1");
					var productTypeId = jsonObj[0].productTypeId;
					loadProduct(trObj, productTypeId);
					
				}
			});
			//$(this).attr("disabled","disabled");
			$(this).hide();
			//获得select下面的所有的option
			$(this).children().each(function(){
				var optVal = $(this).attr("value");
				if(optVal == supplierId){
					$("#supplierText").html($(this).html());
				}
			});
			//因为select被隐藏掉不能在后台接受到值，所以使用input的隐藏域来处理
			$("#supplierHideInput").val(supplierId);
			
		})
		
		/**
		 * 加载类别下的商品, live就是激活拷贝的html代码的事
		 */
		
		
		$(".goodsType").live("change", function(){
			 var trObj = $(this).parent().parent();
			//获得供应商的id
			var productTypeId = $(this).val();
			loadProduct(trObj, productTypeId);
		})
		
		$(".goods").live("change", function(){
			 var trObj = $(this).parent().parent();
			 var trId =  trObj.attr("id");
			 //获得change的商品id
			 var productId = $(this).val();
			 //alert(trId +"      "+productId);
			 //获得每一个商品的下拉框
			 $(".goods").each(function(){
				 
				 //获得每一个商品下拉框的所属行
				 var trObj1 = $(this).parent().parent();
				 //获得所属行的id
				 var trId1 =  trObj1.attr("id");
				 
				 //获得每一个商品的select的值
				 var productId1 = $(this).val();
				 alert(trId1 +"      "+productId1);
				 if(trId != trId1 && productId == productId1){
					 Dialog.alert("当前类别下的商品已经存在");
				 }
			 })
			
		})
		
		/**
		 * 修改数量是发生值的变化
		 */
		$("tbody").find(".num").live("keyup",function(){
			var num = parseInt($(this).val());
			 var trObj = $(this).parent().parent();
			 var inPrice = parseInt(trObj.find(".prices").val());
			
			
			trObj.find(".total").html(inPrice*num+"元");
			//把自定义的属性设置成合计
			trObj.find(".total").attr("val", inPrice*num);
			var totalPrice = 0;
			$("tbody").find(".total").each(function(){
				var total = parseInt($(this).attr("val"));
				totalPrice = totalPrice + total;
			})
			
			$(".all").html(totalPrice+"元");
			
		})
		
		$("tbody").find(".prices").live("keyup",function(){
			var inPrice = parseInt($(this).val());
			 var trObj = $(this).parent().parent();
			 var num = parseInt(trObj.find(".num").val());
			
			
			trObj.find(".total").html(inPrice*num+"元");
			//把自定义的属性设置成合计
			trObj.find(".total").attr("val", inPrice*num);
			var totalPrice = 0;
			$("tbody").find(".total").each(function(){
				var total = parseInt($(this).attr("val"));
				totalPrice = totalPrice + total;
			})
			
			$(".all").html(totalPrice+"元");
			
		})
		
		
		
		
	})
	
	function loadProduct(trObj, productTypeId){
		
		$.ajax({
			url:"${path}/productType_loadpro",
			type:"post",
			dataType:"text",
			data:"productType.productTypeId="+productTypeId,
			success:function(responseText){
				//获得当前行的商品的select
				var productSelect = trObj.find(".goods");
				
				//通过jQuery解析json格式的字符串变成json对象
				productSelect.empty();
				var jsonObj = $.parseJSON(responseText);
				//设置是否是第一个
				var isFirst = true;
				var firstProduct = null;
				for(var i = 0 ; i < jsonObj.length; i++){
					var isSame = false;
					//获得当前表格内的每一个被选中的商品的id
					$(".goods").each(function(){
						//获得每一个被选中的商品的id
						var productId = $(this).val();
						
						if(productId == jsonObj[i].productId){
							isSame = true;
						}
					})
					
					
					if(!isSame){
						if(isFirst){
							isFirst = false;
							//获得排除重复后的第一个
							firstProduct = jsonObj[i];
						}
						productSelect.append("<option value='"+jsonObj[i].productId+"'>"+jsonObj[i].name+"</option>");
					}
					
				}
				//获得到了第一个商品的进货价
				var inPrice = parseInt(firstProduct.inPrice);
				//给单价赋值
				trObj.find(".prices").val(inPrice);
				var num = parseInt(trObj.find(".num").val());
				trObj.find(".total").html(inPrice*num+"元");
				//把自定义的属性设置成合计
				trObj.find(".total").attr("val", inPrice*num);
				var totalPrice = 0;
				$("tbody").find(".total").each(function(){
					var total = parseInt($(this).attr("val"));
					totalPrice = totalPrice + total;
				})
				
				$(".all").html(totalPrice+"元");
				
			}
		});
	}
	
	/**
	 * 删除一行明细
	 */
	function deleteRow(trId){
		if(trId == "#tr1"){
			Dialog.alert("默认的商品不能删除");
		}else{
			$(trId).remove();
		}
	}
	
	
	//提交表单的方法
	function submitOrder(){
		
		
		var result = "";
		
		
			$("#buySubmitOrder").ajaxSubmit({
				dataType:"text",
				async:false,
				success:function(responseText){
					
					result = responseText;
				}
			})
		
		
		
		return result;
	}
</script>
<div class="content-right" >
	<div class="content-r-pic_w">
		<div style="margin:8px auto auto 12px;margin-top:6px">
			<span class="page_title">订单管理</span>
		</div>
	</div>
	<div class="content-text">
		<form id="buySubmitOrder" action="${path }/orderModel_buy_save" method="post">
			<input id="supplierHideInput" type="hidden" name="orderModel.supplier.supplierId">
			<div class="square-o-top">
				<table width="100%" border="0" cellpadding="0" cellspacing="0"
					style="font-size:14px; font-weight:bold; font-family:"黑体";">
					<tr>
						<td width="68px" height="30">供应商：</td>
						<td id="supplierText" width="648px">
							 
							<s:select id="supplierId" cssStyle="width:190px" cssClass="kuan" list="#suppliers" name="orderModel.supplier.supplierId" headerKey="" headerValue="--请选择--" listKey="supplierId" listValue="name"></s:select>
							
						</td>
						<td height="30">
							<a id="add"><img src="${path}/images/can_b_02.gif" border="0" /> </a>
						</td>
					</tr>
				</table>
			</div>
			<!--"square-o-top"end-->
			<div class="square-order"  style="height: 320px; overflow: auto;">
				<table id="order" width="100%" border="1" cellpadding="0" cellspacing="0">
					<tr align="center"
						style="background:url(${path}/images/table_bg.gif) repeat-x;">
						<td width="25%" height="30">商品类别</td>
						<td width="25%">商品名称</td>
						<td width="10%">采购数量</td>
						<td width="15%">单价</td>
						<td width="15%">合计</td>
						<td width="10%">操作</td>
					</tr>
					<tbody>
					
					
					<tr id="tr1" align="center" bgcolor="#FFFFFF">
						<td height="30">
							<select class="goodsType" name="productTypeId" style="width:200px">
								
							</select>
						</td>
						<td>
							<select class="goods" name="productId" style="width:200px">
								
							</select>
						</td>
						<td><input name="nums"  class="num order_num" style="width:67px;border:1px solid black;text-align:right;padding:2px" type="text" value="1"/></td>
						<td><input name="prices" class="prices order_num" style="width:83px;border:1px solid black;text-align:right;padding:2px" type="text" value=""/> 元</td>
						<td class="total" align="right" val="">100.00&nbsp;元</td>
						<td>
							<a class="deleteBtn delete xiu" onclick="deleteRow('#tr1')"><img src="${path}/images/icon_04.gif"/> 删除</a>
						</td>
					</tr>
					
					<tr id="finalTr" align="center"
						style="background:url(${path}/images/table_bg.gif) repeat-x;">
						<td height="30" colspan="4" align="right">总&nbsp;计:&nbsp;</td>
						<td class="all" width="16%" align="right">100.00&nbsp;元</td>
						<td>&nbsp;</td>
					</tr>
					</tbody>
				</table>
				
			</div>
		</form>
	</div>
	
	<div class="content-bbg"></div>
</div>
